import Observer from '@/api/Observer'

class Subject {
  private observers: Array<Observer>
  constructor () {
    this.observers = [] // 观察者列表
  }

  // 添加订阅者
  add (observer: Observer) {
    this.observers.push(observer)
  }

  // 删除...
  remove (observer: Observer) {
    const idx = this.observers.findIndex(item => item === observer)
    idx > -1 && this.observers.splice(idx, 1)
  }

  // 通知
  notify () {
    for (const o of this.observers) {
      o.update()
    }
  }
}

export default Subject
